layui.use(['element', 'jquery'], function() {
    let element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    let $ = layui.jquery;
    /* //监听导航点击
     element.on('nav(demo)', function(elem) {
         //console.log(elem)
         layer.msg(elem.text());
     }); */
    //载入主页前验证token
    let token = localStorage.getItem('token');
    if (token == null) {
        location.href = 'login.html';
    } else {
        //有token，但需要重新验证
        $.ajax({
            method: 'GET',
            url: '/my/userinfo',
            success: function(res) {
                if (res.data.id > 0) {
                    //把用户对象存到localstorage中
                    localStorage.setItem('user', JSON.stringify(res.data));
                    //JSON.stringfy('对象')，先将对象转为字符串存入localstorage中

                    $('#user_pic').attr('src', res.data.user_pic);
                    $('#username').html('欢迎您' + res.data.nickname)
                }
            }

        })
    }


});
